package vote.ai.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.checkerframework.framework.qual.RelevantJavaTypes;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import vote.ai.domain.entity.CorrectTaskDetails;
import vote.ai.domain.enums.CorrectState;
import vote.ai.mapper.CorrectTaskDetailsMapper;
import vote.ai.service.CorrectTaskDetailsService;
import vote.ai.utils.CorrectionUtil;

import java.security.Provider;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Author:han
 * Date: 2024-05-21
 */
@Service
public class CorrectTaskDetailsServiceImpl extends ServiceImpl<CorrectTaskDetailsMapper, CorrectTaskDetails> implements CorrectTaskDetailsService {
    @Resource
    private CorrectTaskDetailsMapper correctTaskDetailsMapper;

    /**
     * 保存任务详情
     * @param taskId
     * @param textSegments
     */

    @Override
    public void batchSaveTask(String taskId, List<CorrectionUtil.TextSegment> textSegments) {
        List<CorrectTaskDetails> detailsList = new ArrayList<>();
        for (CorrectionUtil.TextSegment textSegment : textSegments) {
            CorrectTaskDetails details= CorrectTaskDetails.builder().taskId(taskId)
                    .sort(textSegment.getIndex())
                    .sentence(processText(textSegment.getText()))
                    .startIndex(textSegment.getStartIndex())
                    .endIndex(textSegment.getEndIndex())
                    .build();
            detailsList.add(details);
        }
        correctTaskDetailsMapper.insert(detailsList);
    }

    /**
     * 获取未处理的任务
     * @return
     */
    @Override
    public CorrectTaskDetails getUnParsedTask() {
        LambdaQueryWrapper<CorrectTaskDetails> queryWrapper = new LambdaQueryWrapper<CorrectTaskDetails>().eq(CorrectTaskDetails::getState, CorrectState.UNKNOWN);
        queryWrapper.orderByAsc(CorrectTaskDetails::getCreatedAt).orderByAsc(CorrectTaskDetails::getSort);

        CorrectTaskDetails taskDetails = correctTaskDetailsMapper.selectOne(queryWrapper, false);
        // 判断是否存在
        if (ObjectUtils.isEmpty(taskDetails)) {
            return null;
        }
        taskDetails.setState(CorrectState.PROCESSING);
        if(1!=correctTaskDetailsMapper.updateById(taskDetails)){
            return null;
        }
        return taskDetails;
    }

    @Override
    public Boolean checkTaskFinished(String taskId) {
        // 通过taskId获取当前正在进行与未进行的纠错任务
        LambdaQueryWrapper<CorrectTaskDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CorrectTaskDetails::getTaskId, taskId);
        queryWrapper.in(CorrectTaskDetails::getState, CorrectState.PROCESSING, CorrectState.UNKNOWN);
        return this.count(queryWrapper) == 0;
    }

    /**
     * 处理文本
     * @param text
     * @return
     */
    private static String processText(String text) {
        return text.contains("\n") ? text.replace("\n\n", "<br/>") : text;
    }
}
